// src/stores/permissionMenuStore.js
import {defineStore} from 'pinia'
import * as permissionApi from "@/api/permission";

export const usePermissionStore = defineStore('permission', {
    state: () => ({
        permissionList: [],
        loading: false,
        collapse: false // 示例字段，可按需添加
    }),
    getters: {
        getPermission(state) {
            return state.permissionList
        },
    },
    actions: {
        async fetchPermissions() {
            this.loading = true
            try {
                this.permissionList = await fetchPermissionList()
            } catch (error) {
                console.error('获取权限失败:', error)
            } finally {
                this.loading = false
            }
        },

        toggleCollapse() {
            this.collapse = !this.collapse
        }
    },

})

// 获取权限列表
async function fetchPermissionList(){
    try {
        const data = await permissionApi.getPermissionList();
        return data.data;
    } catch (error) {
        console.error('获取权限列表失败:', error);
        throw error;
    }
}
